const app = getApp()
Page({
  data: {
    like:'good-job-o',
    like_color: '#888888',
    onLike:false,
    store_id: 0,
    card_num:0,
    store: [],
    coupon:[],
    shop:[],
  },
  /**
   * 载入
   */
  onLoad: function (options) {
    app.getParam(options, (rel) => {
      this.setData({
        store_id: rel.store_id,
      })
      this.getStore(rel.store_id);
    });
  },
  //获取首页接口
  getStore: function (store_id) {
    app.api().Get('api/v4/allwin/store/getView',{'store_id': store_id },(result)=>{
      wx.setNavigationBarTitle({
        title: result.data.name
      })
      this.setData({
        store: result.data
      });
    })
    this.getShop();
    this.getCoupon();
    this.getCard();
  },
  //获取赠品
  getCoupon: function () {
    var param = {
      store_id: this.data.store_id,
      page:0
    }
    app.api().Get('api/v4/allwin/store/getCoupon', param,(result) => {
      if (result.code == 200){
        this.setData({
          coupon: result.data.coupon,
          coupon_num: result.data.coupon_num
        });
      }
    })
  },
  //获取商家产品
  getShop: function() {
    var param = {
      store_id: this.data.store_id,
      page: 0 
    }
    app.api().Get('api/v4/allwin/store/getShop',param,(result)=>{
      if (result.code == 200) {
        this.setData({
          shop: result.data.shop,
          shop_num: result.data.shop_num
        });
      }
    })
  },  
  //获取商家产品
  getCard: function() {
    var param = {
      store_id: this.data.store_id,
      signkey: app.util().getRandom(12),
    }
    app.api().Get('api/v4/allwin/card/isCard',param,(result)=>{
      if (result.code == 200) {
        this.setData({
          card_num:result.data
        });
      }
    })
  },
  //领取赠品
  onCoupon: function (e) {
    var coupon_id = e.currentTarget.id;
    var coupon_key = e.currentTarget.dataset.key;
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    var parms = {
      signkey: app.util().getRandom(12),
      coupon_id: coupon_id
    }
    app.api().Post('api/v4/allwin/coupon/buyCoupon', parms,(rel)=>{
      var coupon = this.data.coupon;
      if (rel.data.isbuy == 0){
        app.doWechatPay(rel.data.pay,(res)=>{
          coupon[coupon_key]['state'] = 1;
          this.setData({
            coupon: coupon,
          })
        }, (res)=>{
          wx.showToast({ title: '领取失败',icon: 'none'})
        });
      }else{
        wx.showToast({title: '领取成功',icon: 'success'})
        coupon[coupon_key]['state'] = 1;
        this.setData({
          coupon: coupon,
        })
      }
      wx.hideLoading();
    }) 
  },
  //拨打电话
  callPhone: function (e) {
    app.callPhone(e);
  },
  //图片预览
  previewImage: function (e) {
    var current = e.target.dataset.src;
    var img = this.data.store.imgs;
    var imgs = [];
    for (let i in img) {
      imgs[i] = img[i];
    }
    wx.previewImage({
      current: current,
      urls: imgs
    })
  },
  //点赞
  onLike:function(){
    if ('good-job-o' == this.data.like){
      var param = {
        id: this.data.store_id,
      }
      app.api().Post("api/v4/allwin/store/like",param,(rel)=>{
        this.setData({
          like:'good-job',
          like_color:'#FFA000',
          onLike:true
        })
        setTimeout(()=>{
          this.setData({
            onLike:false
          })
        },600)
      })
    }
  },
  //打开商品
  onShop: function (e) {
    wx.navigateTo({
      url: '../shop/item?id=' + e.currentTarget.dataset.id
    })
  },
  //分享按钮
  onShareAppMessage: function (res) {
    var store = this.data.store;
     return {
       title: store.name,
       imageUrl:store.img,
       path: '/pages/store/views?ucode=' + app.globalData.loginuser.ucode + '&store_id=' + this.data.store_id
     }
  },
  //地图定位
  maps: function () {
    var that = this
    var store = that.data.store;
    var latitude = store.latitude, longitude = store.longitude
    if (app.util().isNull(latitude) || app.util().isNull(longitude)) {
      wx.getLocation({
        type: 'wgs84',
        success: function (res) {
          longitude = res.longitude, latitude = res.latitude;
          wx.openLocation({
            name: store.name,
            longitude: parseFloat(longitude),
            latitude: parseFloat(latitude),
            scale: 18,
            address: store.address
          });
          that.setData({
            latitude: latitude,
            longitude: longitude,
            markers: [{
              latitude: latitude,
              longitude: longitude
            }]
          })
        }
      })
    } else {
      wx.openLocation({
        name: store.name,
        longitude: parseFloat(longitude),
        latitude: parseFloat(latitude),
        scale: 18,
        address: store.address
      });
      that.setData({
        latitude: latitude,
        longitude: longitude,
        markers: [{
          latitude: latitude,
          longitude: longitude
        }]
      })
    }
  }
});